package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 管理员日志
func (*DataParsing) parsingCarLog(catData []string, ftpInfoId int64) []model.DataCar {
	var logDate, LogTime, steamId, gameName, gameId, carType, carName, carId, dataTxt, location string
	var end int
	var dataArr []model.DataCar
	for _, logTxt := range catData {
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//获取类型
		start := strings.Index(logTxt, ": [")
		//fmt.Println("位置1", start)
		if start < 0 {
			carType = ""
		} else {
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				carType = ""
			} else {
				carType = logTxt[start+3 : start+3+end]
			}
		}

		// 获取汽车名称
		start = strings.Index(logTxt, "] ")
		if start < 0 {
			carName = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+2:], ".")
			if end < 0 {
				carName = ""
			} else {
				carName = logTxt[start+2 : start+2+end]
				carName = TruncateStringByCharCount(carName, 50)

				// 输出字节集（以十六进制表示）
				//fmt.Printf("Byte slice: %x\n", byteSlice)

				// 将字节集转换回字符串
				carName = filterInvalidChars(carName)
			}
		}

		// 获取汽车id
		start = strings.Index(logTxt, "Id: ")
		if start < 0 {
			carId = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+4:], ".")
			if end < 0 {
				carId = ""
			} else {
				carId = logTxt[start+4 : start+4+end]
			}
		}

		//截取玩家信息
		start = strings.Index(logTxt, "Owner: ")
		//fmt.Println("位置1", start)
		if start < 0 {
			dataTxt = ""
		} else {
			end = strings.Index(logTxt[start+7:], ".")
			if end < 0 {
				dataTxt = ""
			} else {
				dataTxt = logTxt[start+7 : start+7+end]
			}
		}

		if dataTxt == "N/A" {
			gameName = "N/A"
			steamId = ""
		} else {
			//获取steamId
			start = 0
			end = strings.Index(dataTxt[start:], " (")
			if end < 0 {
				steamId = ""
			} else {
				steamId = dataTxt[:end]
			}

			//游戏名id获取
			start = strings.Index(dataTxt, "(")
			//fmt.Println("位置1", start)
			if start < 0 {
				gameId = ""
			} else {
				end = strings.Index(dataTxt[start+1:], ",")
				if end < 0 {
					gameId = ""
				} else {
					gameId = dataTxt[start+1 : start+1+end]
				}
			}

			// 获取汽车id
			start = strings.Index(dataTxt, ", ")
			if start < 0 {
				gameName = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(dataTxt[start+2:], ")")
				if end < 0 {
					gameName = ""
				} else {
					gameName = dataTxt[start+2 : start+2+end]
					gameName = TruncateStringByCharCount(gameName, 50)

					// 输出字节集（以十六进制表示）
					//fmt.Printf("Byte slice: %x\n", byteSlice)

					// 将字节集转换回字符串
					gameName = filterInvalidChars(gameName)
				}
			}
		}

		start = strings.Index(logTxt, "tion: ")
		if start < 0 {
			location = ""
		} else {
			// 查找第二个单引号的位置
			location = logTxt[start+6:]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, loginType, loginIp, location)

		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		downloadFile := model.DataCar{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId, Type: carType, CarId: carId, CatName: carName, Location: location}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}
